//
//  TouchLayer.m
//  TengineTwo
//
//  Created by StandardUser on 11/02/2012.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "TouchLayer.h"
#import "CCDirector.h"

@implementation TouchLayer

@synthesize delegate;

- (id)init {
    self = [super init];
    if (self) {
        self.isTouchEnabled = YES;
        //self.isAccelerometerEnabled = YES;
    }
    return self;
}

#pragma mark touchdelegate

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    UITouch *touch      = [touches anyObject];
    CGPoint location    = [touch locationInView: [touch view]];
    CGPoint converted   = [[CCDirector sharedDirector] convertToUI:location];
    
    if(delegate)
    {
        if(touches.count > 1)
        {
            [delegate onMultiTouchOn];
        }
        else
        {
            [delegate onTouchOn:converted];
        }
    }
}

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    
    UITouch *touch      = [touches anyObject];
    CGPoint location    = [touch locationInView: [touch view]];
    CGPoint converted   = [[CCDirector sharedDirector] convertToUI:location];
                        
    [delegate onTouchMove:converted];
    
}

-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    
    if(delegate)
    {
        if(touches.count > 1)
        {
            [delegate onMultiTouchOff];
        }
        else
        {
            [delegate onTouchOff];
        }
    }

}

#pragma mark tiltdelegate

- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
{
    float clampedAmount = acceleration.y;
    if(clampedAmount > 1) clampedAmount = 1;
    if(clampedAmount < -1) clampedAmount = -1;
    if(delegate) [delegate onTilt:clampedAmount * -1];
}

@end
